package com.brooks.demo;

import java.lang.ref.SoftReference;

/**
 * 软引用
 * @Author hupengnan
 * @Date 2020/10/13 11:22
 * @Version 1.0
 */
public class M_SoftReference {
    public static void main(String[] args) {
        SoftReference<byte[]> m = new SoftReference<>(new byte[1024*1024*10]);
        //去除强引用
        //m = null;
        System.out.println(m.get());
        System.gc();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(m.get());
        //再分配一个数组，heap将装不下，这时候系统会垃圾回收，先回收一次，如果不够，会吧软引用干掉
        byte[] b = new byte[1024*1024*12];
        System.out.println(m.get());
    }
}
